package com.quanquan.user.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.quanquan.user.dto.DataDTO;
import com.quanquan.user.entity.IntegralTradingDetails;
import com.quanquan.user.entity.SelfHelpIntegral;
import com.quanquan.user.entity.User;
import com.quanquan.user.repository.IntegralDetailsRepository;
import com.quanquan.user.repository.SelfHelpIntegralRepository;
import com.quanquan.user.repository.SignInRepository;
import com.quanquan.user.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import utils.Request;
import utils.StringUtils;

import java.util.List;

/**
 * @ClassName:DataService
 * @Description:TODO
 * @Author:xiongjunbao
 * @Date:2019/5/9 11:00
 * @Version 1.0
 */
@Service
public class DataService {

    @Value("${module-url.storeUrl}")
    private String storeUrl;

    private final UserRepository userRepository;
    private final IntegralDetailsRepository integralDetailsRepository;
    private final SignInRepository signInRepository;
    private final SelfHelpIntegralRepository selfHelpIntegralRepository;

    @Autowired
    public DataService(UserRepository userRepository, IntegralDetailsRepository integralDetailsRepository, SignInRepository signInRepository, SelfHelpIntegralRepository selfHelpIntegralRepository) {
        this.userRepository = userRepository;
        this.integralDetailsRepository = integralDetailsRepository;
        this.signInRepository = signInRepository;
        this.selfHelpIntegralRepository = selfHelpIntegralRepository;
    }

    public DataDTO findDatas() {
        DataDTO dataDTO = new DataDTO();
        String json = Request.postJson(storeUrl + "coupon/coupon_count", null);
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        if (resCode == 0) {
            String resDataStr = jsonObject.getString("data");
            if (StringUtils.isNotEmpty(resDataStr)) {
                Integer getCouponCount = (Integer) JSON.parseObject(resDataStr).get("getCouponCount");
                Integer useCouponCount = (Integer) JSON.parseObject(resDataStr).get("useCouponCount");
                Integer getParkingCoupon = (Integer) JSON.parseObject(resDataStr).get("getParkingCoupon");
                Integer useParkingCoupon = (Integer) JSON.parseObject(resDataStr).get("useParkingCoupon");
                if (getCouponCount == null) {
                    dataDTO.setCouponNum(0);
                } else {
                    dataDTO.setCouponNum(getCouponCount);
                }
                if (useCouponCount == null) {
                    dataDTO.setCouponUseNum(0);
                } else {
                    dataDTO.setCouponUseNum(useCouponCount);
                }
                if (getParkingCoupon == null) {
                    dataDTO.setParkingCouponNum(0);
                } else {
                    dataDTO.setParkingCouponNum(getParkingCoupon);
                }
                if (useParkingCoupon == null) {
                    dataDTO.setParkingCouponUseNum(0);
                } else {
                    dataDTO.setParkingCouponUseNum(useParkingCoupon);
                }
            }
        }
        dataDTO.setUserNum(userRepository.findAllUser().size());
        dataDTO.setSigninNum(signInRepository.findAllSignin().size());
        //积分获得总数
        Integer IntegralInNum = 0;
        List<IntegralTradingDetails> integralTradingDetails = integralDetailsRepository.findAllIn();
        for (IntegralTradingDetails integralTradingDetails1 : integralTradingDetails) {
            IntegralInNum = Integer.parseInt(integralTradingDetails1.getIntegralPoint()) + IntegralInNum;
        }
        dataDTO.setUserCentNum(IntegralInNum);
        //积分使用总数
        Integer IntegralOutNum = 0;
        List<IntegralTradingDetails> integralTradingDetails1 = integralDetailsRepository.findAllOut();
        for (IntegralTradingDetails integralTradingDetails2 : integralTradingDetails1) {
            IntegralOutNum = Integer.parseInt(integralTradingDetails2.getIntegralPoint()) + IntegralOutNum;
        }
        dataDTO.setUserCentOutNum(IntegralOutNum);
        //用户总积分
        Integer userSumIntergral = 0;
        List<User> users = userRepository.findAllByRemoveOrderByCreateTimeDesc(0);
        for (User user : users) {
            userSumIntergral = user.getIntegral() + userSumIntergral;
        }
        dataDTO.setUserSumIntergral(userSumIntergral);

        Double money = 0.0;
        List<SelfHelpIntegral> selfHelpIntegrals = selfHelpIntegralRepository.findAllSelfHelpIntegral();
        for (SelfHelpIntegral selfHelpIntegral : selfHelpIntegrals) {
            if (selfHelpIntegral == null) {
                continue;
            }
            if (selfHelpIntegral.getMoney() != null) {
                money = Double.parseDouble(selfHelpIntegral.getMoney()) + money;
            }
        }
        dataDTO.setMoney(money);

        dataDTO.setIntegralNum(selfHelpIntegrals.size());
        System.out.println(dataDTO);
        return dataDTO;
    }

}
